//
//  ApplyRefundVC.m
//  phmall
//
//  Created by admin on 2017/12/16.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ApplyRefundVC.h"
#import "RefundCell.h"
#import "EvaluateText.h"
#import "ZLPhotoActionSheet.h"
#import "ZLCustomCamera.h"
@interface ApplyRefundVC ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,strong)UITableView *myTableview;
@property(nonatomic,strong)NSMutableArray *photoArray;


@end

@implementation ApplyRefundVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myTableview];
    
    self.title=Localized(@"申请退款");
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(-self.tabBar_height-44).priorityHigh();
    }];
    // Do any additional setup after loading the view.
    
    [self addFooterView];
}
-(void)addFooterView{
    //底部背景
    UIView *footerView=[UIView new];
    footerView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:footerView];
    
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-self.tabBar_height);
        make.left.right.offset(0);
        make.height.offset(49);
    }];
    
    //立即购买
    UIButton *purchase=[UIButton buttonWithType:UIButtonTypeCustom];
    [purchase setTitle:@"提交申请" forState:UIControlStateNormal];
    purchase.backgroundColor=[UIColor colorWithHexString:@"#fb6579"];
    [footerView addSubview:purchase];
    purchase.titleLabel.font=[UIFont systemFontOfSize:15];
    [purchase mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(footerView);
        make.width.offset(SCREEN_WIDTH/5*2);
    }];
    
    UILabel *number=[[UILabel alloc]init];
    [footerView addSubview:number];
    number.text=@"(含运费)";
    number.font=[UIFont systemFontOfSize:10];
    [number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.bottom.offset(0);
        make.height.offset(21);
    }];
    
    UILabel *price=[[UILabel alloc]init];
    [footerView addSubview:price];
    price.text=@"退款:₱90909090";
    price.font=[UIFont systemFontOfSize:16];
    price.textColor=[UIColor orangeColor];
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(0);
        make.height.offset(28);
    }];
    
    UIView *line=[[UIView alloc]init];
    [footerView addSubview:line];
    line.backgroundColor=[UIColor colorWithHexString:@"#ECEBEC"];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(0.5);
    }];
}
#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    OrderModel *model=self.orderList[section];
    //    NSMutableArray *data=model.goods_list;
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 294;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 54;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 330)];
    view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    UIView *causeView=[[UIView alloc]init];
    causeView.backgroundColor=[UIColor whiteColor];
    [view addSubview:causeView];
    [causeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view);
        make.top.equalTo(view).offset(10);
        
        make.height.offset(40);
    }];
    
    UILabel *select=[[UILabel alloc]init];
    select.text=@"请选择退款原因";
    select.font=[UIFont systemFontOfSize:12];
    [view addSubview:select];
    [select mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(causeView).offset(10);
        make.centerY.equalTo(causeView);
    }];
    
    UIImageView *img=[[UIImageView alloc]init];
    img.image=[UIImage imageNamed:@"前往"];
    [view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(causeView).offset(-10);
        make.centerY.equalTo(causeView);
        make.height.offset(15);
        make.width.offset(15);
    }];
    
    UITapGestureRecognizer * causeTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
    causeTap.numberOfTouchesRequired = 1; //手指数
    causeTap.numberOfTapsRequired = 1; //tap次数
    causeTap.delegate= self;
    [causeView addGestureRecognizer:causeTap];
    
    
    UIView *back=[[UIView alloc]init];
    back.backgroundColor=[UIColor whiteColor];
    [view addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(causeView.mas_bottom).offset(10);
        make.left.equalTo(view).offset(0);
        make.right.equalTo(view).offset(0);
        make.height.offset(190);
    }];
    
    EvaluateText *textView=[[EvaluateText alloc]init];
    textView.placeholder=@"在这里留下你的想法吧~~~";
    [back addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.equalTo(back).offset(10);
        make.right.equalTo(back).offset(-10);
        make.height.offset(100);
    }];
    
    for (int i=0; i<self.photoArray.count+1; i++) {
        UIImageView *imageView=[[UIImageView alloc]init];
        if (i==self.photoArray.count) {
            imageView.image=[UIImage imageNamed:@"上传照片"];
        }else{
            imageView.image=self.photoArray[i];
        }
        imageView.contentMode=UIViewContentModeScaleAspectFill;
        ViewBorderRadius(imageView, 0, 0.5, [UIColor groupTableViewBackgroundColor]);
        [back addSubview:imageView];
        [imageView  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(i*(80)+10);
            make.top.equalTo(textView.mas_bottom).offset(10);
            make.width.height.offset(70);
        }];
        imageView.tag=100+i;
     
        UITapGestureRecognizer *imageTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapAction:)];
        imageTap.numberOfTouchesRequired = 1; //手指数
        imageTap.numberOfTapsRequired = 1; //tap次数
        imageView.userInteractionEnabled=YES;
        imageTap.delegate= self;
        [imageView addGestureRecognizer:imageTap];
        
        if (i!=self.photoArray.count) {
            UIButton *cancel=[UIButton buttonWithType:UIButtonTypeCustom];
            [cancel setImage:[UIImage imageNamed:@"取消"] forState:UIControlStateNormal];
            [cancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
            cancel.tag=110+i;
            [back addSubview:cancel];
            [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(imageView.mas_right);
                make.centerY.equalTo(imageView.mas_top);
                make.width.height.offset(15);
            }];
        }
    }
    
    UIButton *hint=[UIButton buttonWithType:UIButtonTypeCustom];
    [hint setTitle:@"上传图片最多三张" forState:UIControlStateNormal];
    [hint setImage:[UIImage imageNamed:@"提示"] forState:UIControlStateNormal];
    hint.titleLabel.font=[UIFont systemFontOfSize:13];
    [hint setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:hint];
    [hint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.equalTo(back.mas_bottom).offset(22);
    }];
    [hint layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
    return view;
}
-(void)cancelAction:(UIButton *)sender{
    [self.photoArray removeObjectAtIndex:sender.tag-110];
    [self.myTableview  reloadData];
}
-(void)imageTapAction:(UITapGestureRecognizer *)sender{
    if (sender.view.tag-100<self.photoArray.count) {
        [[self getPas] previewPhotos:self.photoArray index:sender.view.tag-100 hideToolBar:YES complete:^(NSArray * _Nonnull photos) {
            NSLog(@"%@", photos);
        }];
        return;
    }
    if (self.photoArray.count==4) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加错误" message:@"最多只能添加3张图片" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self.navigationController presentViewController:alertController animated:YES completion:nil];
        
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self photograph];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self showWithPreview];
        }]];
        [self.navigationController presentViewController:alertController animated:YES completion:nil];
    }
}
- (void)showWithPreview{
    [[self getPas] showPhotoLibrary];
}
- (ZLPhotoActionSheet *)getPas
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    
#pragma mark - 参数配置 optional，可直接使用 defaultPhotoConfiguration
    //设置相册内部显示拍照按钮
    actionSheet.configuration.allowTakePhotoInLibrary = YES;
    //设置在内部拍照按钮上实时显示相机俘获画面
    actionSheet.configuration.showCaptureImageOnTakePhotoBtn = YES;
    //设置照片最大预览数
    actionSheet.configuration.maxPreviewCount = 10;
    //设置照片最大选择数
    actionSheet.configuration.maxSelectCount = 4-self.photoArray.count;
    //设置允许选择的视频最大时长
    actionSheet.configuration.maxVideoDuration = 10;
    //设置照片cell弧度
    actionSheet.configuration.cellCornerRadio = 0;
    //单选模式是否显示选择按钮
    //    actionSheet.configuration.showSelectBtn = YES;
    //是否在选择图片后直接进入编辑界面
    actionSheet.configuration.editAfterSelectThumbnailImage = NO;
    //设置编辑比例
    //    actionSheet.configuration.clipRatios = @[GetClipRatio(7, 1)];
    //是否在已选择照片上显示遮罩层
    actionSheet.configuration.showSelectedMask = NO;
    //颜色，状态栏样式
    actionSheet.configuration.selectedMaskColor = [UIColor redColor];
    actionSheet.configuration.navBarColor = [UIColor whiteColor];
    actionSheet.configuration.navTitleColor = [UIColor blackColor];
    actionSheet.configuration.bottomBtnsNormalTitleColor =  [UIColor blackColor];
    actionSheet.configuration.bottomBtnsDisableBgColor =  [UIColor blackColor];
    actionSheet.configuration.bottomViewBgColor = [UIColor whiteColor];
    actionSheet.configuration.statusBarStyle = UIStatusBarStyleDefault;
    //是否允许框架解析图片
    actionSheet.configuration.shouldAnialysisAsset = YES;
    //框架语言
    //    actionSheet.configuration.languageType = self.languageSegment.selectedSegmentIndex;
    //是否使用系统相机
    //    actionSheet.configuration.useSystemCamera = YES;
    //    actionSheet.configuration.sessionPreset = ZLCaptureSessionPreset1920x1080;
    //    actionSheet.configuration.exportVideoType = ZLExportVideoTypeMp4;
    
#pragma mark - required
    //如果调用的方法没有传sender，则该属性必须提前赋值
    actionSheet.sender = self;
    //记录上次选择的图片
    //    actionSheet.arrSelectedAssets = self.rememberLastSelSwitch.isOn&&self.maxSelCountTextField.text.integerValue>1 ? self.lastSelectAssets : nil;
    
    zl_weakify(self);
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        zl_strongify(weakSelf);
        for (UIImage *image in images) {
            [strongSelf.photoArray addObject:image];
        }
        [strongSelf.myTableview reloadData];
        NSLog(@"image:%@", images);
    }];
    
    return actionSheet;
}
-(void)photograph{
#pragma mark - UIButton Action

        ZLCustomCamera *camera = [[ZLCustomCamera alloc] init];
        camera.allowRecordVideo =NO ;
        camera.sessionPreset = ZLCaptureSessionPreset1280x720;
//        camera.videoType = self.configuration.exportVideoType;
//        camera.circleProgressColor = self.configuration.bottomBtnsNormalTitleColor;
//        camera.maxRecordDuration = self.configuration.maxRecordDuration;
        zl_weakify(self);
        camera.doneBlock = ^(UIImage *image, NSURL *videoUrl) {
            zl_strongify(weakSelf);
            [strongSelf.photoArray addObject:image];
            [strongSelf.myTableview reloadData];
        };
        [self showDetailViewController:camera sender:nil];
 }
-(void)tapAvatarView:(UITapGestureRecognizer *)tap{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击取消");
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"我不想买了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击确认");
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"信息填错,重新拍" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击确认");
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"卖家迟迟不发货" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击确认");
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"误点错拍" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击确认");
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"其他原因" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击确认");
    }]];
    
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    //    OrderModel *model=self.orderList[section];
    
    UIView *back=[[UIView alloc]init];
    back.backgroundColor=[UIColor whiteColor];
    [view addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(10);
        make.left.equalTo(view).offset(0);
        make.right.equalTo(view).offset(0);
        make.height.offset(44);
    }];
    
    UILabel *number=[[UILabel alloc]init];
    number.text=@"订单编号:OD150753966532";
    number.font=[UIFont systemFontOfSize:16];
    [back addSubview:number];
    [number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.equalTo(back);
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [back addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(0);
        make.right.equalTo(view).offset(0);
        make.bottom.equalTo(view);
        make.height.offset(1);
    }];
    
    return view;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RefundCell *cell=[RefundCell theShareCellWithTableView:tableView];
    //    OrderModel *model=self.orderList[indexPath.section];
    //    NSMutableArray *data=model.goods_list;
    //    GoodsListModel *goodsModel=data[indexPath.row];
    //    cell.model=goodsModel;
    cell.apply.hidden=YES;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    self.hidesBottomBarWhenPushed=YES;
    //    OrderdetailVC *evaluate=[[OrderdetailVC alloc]init];
    //    [self.navigationController pushViewController:evaluate animated:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
//MARK:-GETTER
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}
-(NSMutableArray *)photoArray{
    if (!_photoArray) {
        _photoArray=[[NSMutableArray alloc]init];
        
    }
    return _photoArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

